package oas.web;

import oas.db.LeaveRepository;
import oas.db.UserRepository;
import oas.domain.User;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.logging.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户操作Controller
 *
 * @author liyue
 * @version v1.2.0
 */
@Controller
@RequestMapping("/user")
@SessionAttributes({"user"})
public class UserController {

    private final static Log log = LogFactory.getLog(UserController.class);

    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/center", method = RequestMethod.GET)
    public String showCenter() {
        return "userCenter";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String showUpdate(Model model) {
        Map<String, String> genderList = new HashMap<String, String>();
        genderList.put("Male", "Male");
        genderList.put("Female", "Female");
        model.addAttribute("genderList", genderList);
        return "updateForm";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(User user, HttpSession session) {
        User customer = (User) session.getAttribute("user");

        customer.setAge(user.getAge());
        customer.setEmail(user.getEmail());
        customer.setGender(user.getGender());
        customer.setPhone(user.getPhone());
        customer.setFullName(user.getFullName());

        userRepository.updateUser(customer);

        session.setAttribute("user", customer);
        return "updateSuccessful";
    }

    @RequestMapping(value = "/password", method = RequestMethod.GET)
    public String showChangePwd() {
        return "updatePwdForm";
    }

    @RequestMapping(value = "/password", method = RequestMethod.POST)
    public String ChangePwd(@RequestParam(value = "oldPassword", defaultValue = "") String oldPassword,
                            @RequestParam(value = "newPassword", defaultValue = "") String newPassword,
                            HttpSession session) {

        User customer = (User) session.getAttribute("user");

        log.debug(oldPassword + ", " + newPassword);
        byte[] decodedOldPwd = Base64.getDecoder().decode(oldPassword);
        byte[] decodedNewPwd = Base64.getDecoder().decode(newPassword);
        oldPassword = new String(decodedOldPwd);
        newPassword = new String(decodedNewPwd);

        log.debug(oldPassword + ", " + newPassword);
        boolean result = userRepository.changePassword(customer.getId(), oldPassword, newPassword);

        if (result) {
            log.debug("修改成功");
        } else {
            log.debug("修改失败");
        }
        return "redirect:/";
    }
}
